package com.fudan.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.fudan.dao.UserDaoImpl;
import com.fudan.dto.entities.User;

@Transactional
@Service
public class UserServiceImpl {
	
	@Autowired
	private UserDaoImpl userDao;
	
	
	public UserServiceImpl(){
	}
	
	public List<User> getAllUsers(){
		return userDao.getAllUser();
	}
	
	public User getUser(Integer userId){
		User user = userDao.getUserbyId(userId);
		return user;
	}
	
	public User getUserByUsername(String username){
		if (StringUtils.isEmpty(username)){
			return null;
		}
		
		List<User> users = userDao.getUserByUsername(username);
		
		if (users.isEmpty()) {
			return null;
		}else {
			return users.get(0);
		}
	}
	
	public User createTeacher(String username, String password){		
		User user = userDao.createUser(username, password, User.TEACHER);
		return user;
	}
	
	public User createStudent(String username, String password){
		User user = userDao.createUser(username, password, User.STUDENT);
		return user;
	}

}
